home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / c / pro22 / cbkeylas.c < prev    next >
Text File  |  1990-06-20  |  3KB  |  135 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbkeylas.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <stdlib.h>*/
  9. /*#include <string.h>*/
  10.  
  11. /* library headers */
  12. #include <blkio.h>
  13. #include <btree.h>
  14. #include <lseq.h>
  15.  
  16. /* local headers */
  17. #include "cbase_.h"
  18.  
  19. /*man---------------------------------------------------------------------------
  20. NAME
  21.      cbkeylast - last cbase key
  22.  
  23. SYNOPSIS
  24.      #include <cbase.h>
  25.  
  26.      int cbkeylast(cbp, field)
  27.      cbase_t *cbp;
  28.      int field;
  29.  
  30. DESCRIPTION
  31.      The cbkeylast function positions the key cursor of the specified
  32.      field in cbase cbp to the last key.  The record cursor of cbp is
  33.      positioned to the record associated with that key.  Other key
  34.      cursors are not affected.
  35.  
  36.      cbkeylast will fail if one or more of the following is true:
  37.  
  38.      [EINVAL]       cbp is not a valid cbase pointer.
  39.      [EINVAL]       field is not a valid field number for
  40.                     cbase cbp.
  41.      [CBELOCK]      cbp is not locked.
  42.      [CBENKEY]      field is not a key.
  43.      [CBENOPEN]     cbp is not open.
  44.      [CBENREC]      cbp is empty.
  45.  
  46. SEE ALSO
  47.      cbkeyfirst, cbkeynext, cbkeyprev, cbreccnt, cbreclast.
  48.  
  49. DIAGNOSTICS
  50.      Upon successful completion, a value of 0 is returned.  Otherwise,
  51.      a value of -1 is returned, and errno set to indicate the error.
  52.  
  53. ------------------------------------------------------------------------------*/
  54. int cbkeylast(cbp, field)
  55. cbase_t *cbp;
  56. int field;
  57. {
  58.     void * buf = NULL;
  59.     cbrpos_t cbrpos = NIL;
  60.     lspos_t lspos = NIL;
  61.  
  62.     /* validate arguments */
  63.     if (!cb_valid(cbp)) {
  64.         errno = EINVAL;
  65.         return -1;
  66.     }
  67.  
  68.     /* check if not open */
  69.     if (!(cbp->flags & CBOPEN)) {
  70.         errno = CBENOPEN;
  71.         return -1;
  72.     }
  73.  
  74.     /* validate arguments */
  75.     if (field < 0 || field >= cbp->fldc) {
  76.         errno = EINVAL;
  77.         return -1;
  78.     }
  79.  
  80.     /* check if field not a key */
  81.     if (!(cbp->fldv[field].flags & CB_FKEY)) {
  82.         errno = CBENKEY;
  83.         return -1;
  84.     }
  85.  
  86.     /* check if not locked */
  87.     if (!(cbp->flags & CBLOCKS)) {
  88.         errno = CBELOCK;
  89.         return -1;
  90.     }
  91.  
  92.     /* check if cbase empty */
  93.     if (lsreccnt(cbp->lsp) == 0) {
  94.         errno = CBENREC;
  95.         return -1;
  96.     }
  97.  
  98.     /* set key cursor to last key */
  99.     if (btlast(cbp->btpv[field]) == -1) {
  100.         CBEPRINT;
  101.         return -1;
  102.     }
  103.  
  104.     /* get record position */
  105.     if (btkeysize(cbp->btpv[field]) != (cbp->fldv[field].len + sizeof(cbrpos_t))) {
  106.         CBEPRINT;
  107.         errno = CBEPANIC;
  108.         return -1;
  109.     }
  110.     buf = calloc((size_t)1, btkeysize(cbp->btpv[field]));
  111.     if (buf == NULL) {
  112.         CBEPRINT;
  113.         errno = ENOMEM;
  114.         return -1;
  115.     }
  116.     if (btgetk(cbp->btpv[field], buf) == -1) {
  117.         CBEPRINT;
  118.         free(buf);
  119.         return -1;
  120.     }
  121.     memcpy(&cbrpos, ((char *)buf + cbp->fldv[field].len), sizeof(cbrpos));
  122.     free(buf);
  123.     buf = NULL;
  124.  
  125.     /* set record cursor */
  126.     lspos = cbrpos;
  127.     if (lssetcur(cbp->lsp, &lspos) == -1) {
  128.         CBEPRINT;
  129.         return -1;
  130.     }
  131.  
  132.     errno = 0;
  133.     return 0;
  134. }
  135.